package com.unieagle.exam.http;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;

import com.google.gson.Gson;


public class BusinessConfig {
    private static final String HOST_T = "http://unieagle.net:4000";

    private static final String HOST = HOST_T;

    private static final String REGISTER_URI = "/api/users/signup.json";
    private static final String REGISTER_CONTENT = "{\"user\":{\"name\":\"%1$s\",\"certificate\":\"%2$s\",\"phone\":\"%3$s\",\"getui_token\":\"%4$s\",\"device_type\":\"android\"}}";

    private static final String VERIFICATION_CODE_URI = "/api/users/send_verification_code.json";
    private static final String VERIFICATION_CODE_CONTENT = "{\"phone\":\"%1$s\"}";

    private static final String SIGNIN_URI = "/api/users/signin.json";
    private static final String SIGNIN_URI_CONTENT = "{\"phone\":\"%1$s\",\"verification_code\":\"%2$s\",\"getui_token\":\"%3$s\",\"device_type\":\"android\"}";

    private static final String UPLOAD_IMAGE_URI = "/api/users/%1$s/upload_image.json";
    private static final String UPLOAD_IMAGE_CONTENT = "{\"access_token\":\"%1$s\",\"image_type\":\"%2$s\",\"image_data\":\"%3$s\"}";
    public static final String UPLOAD_IMAGE_TYPE_FRONT = "certificate_image_front";
    public static final String UPLOAD_IMAGE_TYPE_BACK = "certificate_image_back";
    public static final String UPLOAD_IMAGE_TYPE_AVATAR = "avatar";

    private static final String REQUEST_ACTIVATION_URI = "/api/users/%1$s/request_activation.json";
    private static final String REQUEST_ACTIVATION_CONTENT = "{\"access_token\":\"%1$s\"}";

    private static final String SELECT_SUBJECTS_URI = "/api/users/%1$s/select_subjects.json";
    private static final SELECT_SUBJECTS_CONTENT SELECT_SUBJECTS_CONTENT_OBJ = new SELECT_SUBJECTS_CONTENT();
    private static class SELECT_SUBJECTS_CONTENT{
        private String access_token;
        private Collection<Integer> subjects;
        private Boolean remove;
    }

    private static final String CHECK_VERSION_FORMAT = "/api/check_version.json?client=%1$s&client_version=%2$s";

    private static final String CHECK_VERSION = "/api/check_version.json";
    private static final String CHECK_VERSION_CONTENT = "{\"client\":\"%1$s\",\"client_version\":\"%2$s\"}";


    public static String getRegisterUri(){
        return HOST + REGISTER_URI;
    }

    public static String getRegisterContent(String name, String certificate,String phone, String pushToken){
        return String.format(REGISTER_CONTENT, name,certificate, phone, pushToken);
    }

    public static String getVerificationCodeUri(){
        return HOST + VERIFICATION_CODE_URI;
    }
    public static String getVerificationCodeContent(String phone){
        return String.format(VERIFICATION_CODE_CONTENT, phone);
    }
    public static String getSigninUri(){
        return HOST + SIGNIN_URI;
    }

    public static String getSigninContent(String phone, String verificationCode, String pushToken){
        return String.format(SIGNIN_URI_CONTENT, phone, verificationCode, pushToken);
    }

    public static String getUploadImageUri(String id){
        return String.format(HOST + UPLOAD_IMAGE_URI, id);
    }

    public static String getUploadImageContent(String token, String imageType, String imageData){
        return String.format(UPLOAD_IMAGE_CONTENT, token,imageType,imageData);
    }

    public static String getRequestActivationUri(String id){
        return String.format(HOST + REQUEST_ACTIVATION_URI, id);
    }

    public static String getRequestActivationContent(String token){
        return String.format(REQUEST_ACTIVATION_CONTENT, token);
    }

    public static String getSelectSubjectsUri(String id){
        return String.format(HOST + SELECT_SUBJECTS_URI, id);
    }

    public static String getSelectSubjectsContent(String token, Collection<Integer> subjects, Boolean remove){
        String rel;
        synchronized (SELECT_SUBJECTS_CONTENT_OBJ) {
            SELECT_SUBJECTS_CONTENT_OBJ.access_token = token;
            SELECT_SUBJECTS_CONTENT_OBJ.subjects = subjects;
            SELECT_SUBJECTS_CONTENT_OBJ.remove = remove;
            rel = new Gson().toJson(SELECT_SUBJECTS_CONTENT_OBJ);
        }
        return rel;
    }

    public static String getCheckVersionUri(String os,String currentVersion){
        try {
            currentVersion = URLEncoder.encode(currentVersion, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return String.format(HOST + CHECK_VERSION_FORMAT,os, currentVersion);
    }

    public static String getCheckVersionUri(){
        return HOST + CHECK_VERSION;
    }

    public static String getCheckVersionContent(String os,String currentVersion){
        return String.format(CHECK_VERSION_CONTENT, os, currentVersion);
    }
}
